package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.AllMember;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = memberService.findPage(queryPageBean);
        return page;
    }

    //新增会员
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member) {
        try {
            memberService.addMember(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }


    //回显会员数据
    @RequestMapping("/findByMember")
    public Result findByMember(Integer id) {
        try {
            Member all = memberService.findByMember(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Member member) {
        try {
            memberService.editMember(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("/deleteMember")
    public Result deleteMember(Integer id) {
        try {
            memberService.deleteMember(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //批量导出数据
    @RequestMapping("/exportExcel")
    public void export(Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws Exception {



        //获取excel模板文件的绝对路径
        String excelTemplatePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member.xlsx";

        //读取模板文件,创建excel表格对象
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelTemplatePath)));

        //设置单元格居中
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中

        //获取excel文件的第一张报表
        XSSFSheet sheet = workbook.getSheetAt(0);
        //自动适应行宽
        sheet.autoSizeColumn(1);



    }
}
